//
//  TMItemDetailController.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/7/11.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMItemDetailController.h"
#import "TMItemDetailCell.h"
#import "TMHomeViewModel.h"
#import "TMCommodityDetailController.h"
#import "TMCourseDetailModel.h"
@interface TMItemDetailController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *itemCollection;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, strong) TMHomeViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong)WZNoResultView *noResultView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalNum;
@end

@implementation TMItemDetailController
- (NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.listArray removeAllObjects];
    [self.viewModel getTypeWithParam:self.type AndPage:1 Success:^(id responseObject,id total) {
        self.totalNum = [total integerValue];
        self.listArray = responseObject;
        [self noResultViewShowORNot];
        [self.itemCollection reloadData];
    } Failed:^(NSString *error) {
        [self noResultViewShowORNot];
        [self.itemCollection reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[TMHomeViewModel alloc]init];
    self.pageNum = 1;
    self.itemCollection.backgroundColor = [UIColor hx_colorWithHexRGBAString:@"#F5F5F5"];
    self.itemCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNum = 1;
        [self.viewModel getTypeWithParam:self.type AndPage:self.pageNum Success:^(id responseObject,id total) {
            self.totalNum = [total integerValue];
            self.listArray = responseObject;
            [self noResultViewShowORNot];
            [self.itemCollection reloadData];
            [self.itemCollection.mj_header endRefreshing];
            [self.itemCollection.mj_footer endRefreshing];
        } Failed:^(NSString *error) {
            [self noResultViewShowORNot];
            [self.itemCollection reloadData];
            [self.itemCollection.mj_header endRefreshing];
            [self.itemCollection.mj_footer endRefreshing];
        }];
    }];
    self.itemCollection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.pageNum ++;
        if (self.pageNum > self.totalNum) {
            [self.itemCollection.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        [self.viewModel getTypeWithParam:self.type AndPage:self.pageNum  Success:^(id responseObject,id total) {
            self.listArray = responseObject;
            [self noResultViewShowORNot];
            [self.itemCollection reloadData];
            [self.itemCollection.mj_header endRefreshing];
            [self.itemCollection.mj_footer endRefreshing];
        } Failed:^(NSString *error) {
            if (self.pageNum > 1) {
                self.pageNum--;
            }
            [self noResultViewShowORNot];
            [self.itemCollection reloadData];
            [self.itemCollection.mj_header endRefreshing];
            [self.itemCollection.mj_footer endRefreshing];
        }];
    }];
}
-(void)setItemCollection:(UICollectionView *)itemCollection{
    _itemCollection = itemCollection;
    _itemCollection.delegate = self;
    _itemCollection.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 12.;
    flowLayout.minimumInteritemSpacing = 0.;
    _itemSize = CGSizeMake(169 * kWindowW /375, 197 * kWindowW /375);
    _itemCollection.collectionViewLayout = flowLayout;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(9, 12, 0, 12);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TMItemDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMItemDetailCell" forIndexPath:indexPath];
    cell.model = self.listArray[indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
    vc.isDiscount = NO;
    TMCourseDetailModel *model = self.listArray[indexPath.row];
    vc.rid = model.cid;
    [self.navigationController pushViewController:vc animated:YES];
}
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        [self.itemCollection addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
@end
